<template>
  <div class="basic-layout">
    <div :class="['nav-side', isCollapse ? 'fold' : 'unfold']">
      <!-- 系统LOGO -->
      <div class="logo">
        <img src="../assets/logo.png" alt="" />
        <span>Manager</span>
      </div>
      <!-- 导航菜单 -->
      <el-menu
        :default-active="activeMenu"
        active-text-color="#ffd04b"
        background-color="#001529"
        text-color="#fff"
        :collapse="isCollapse"
        router
        class="nav-menu"
      >
        <tree-menu :userMenu="userMenu" ref="treeMenuRef" />
      </el-menu>
    </div>
    <div :class="['content-right', isCollapse ? 'fold' : 'unfold']">
      <div class="nav-top">
        <div class="nav-left">
          <div class="menu-fold" @click="toggle">
            <i class="el-icon-s-fold"></i>
          </div>
          <div class="bread">
            <BreadCrumb />
          </div>
        </div>
        <div class="user-info">
          <!-- <el-badge :is-dot="noticeCount > 0 ? true : false" class="notice"> -->
          <el-badge
            :hidden="noticeCount < 1"
            :value="noticeCount"
            :max="10"
            class="notice"
            @click="clickNoticeCount"
          >
            <i size="mini" class="el-icon-bell"></i>
          </el-badge>
          <el-dropdown @command="handleLogout" trigger="click">
            <span class="user-link">
              {{ userInfo.userName }}
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="email"
                  >邮箱：{{ userInfo.userEmail }}
                </el-dropdown-item>
                <el-dropdown-item command="logout">退出</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
      <nav-tabs />
      <div class="wrapper">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, toRefs, computed, ref } from 'vue';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';
import TreeMenu from './TreeMenu.vue';
import BreadCrumb from './BreadCrumb.vue';
import NavTabs from './NavTabs.vue';
import $api from '../api';

const store = useStore();
const router = useRouter();
const treeMenuRef = ref();

const data = reactive({
  isCollapse: false, // 是否折叠
  userMenu: [],
  activeMenu: location.hash.slice(1), // '/welcome'
});
const userInfo = store.state.userInfo;

// 动态计算通知数
const noticeCount = computed(() => {
  return store.state.noticeCount;
});
const activeMenu = computed(() => {
  return store.state.activeTab.name;
});

const toggle = () => {
  isCollapse.value = !isCollapse.value;
};
// 跳转待审批
const clickNoticeCount = () => {
  treeMenuRef.value.clickMenu('待审批', '/audit/approve');
  router.push('/audit/approve');
};

// 退出登录
const handleLogout = (key) => {
  if (key == 'email') return;
  store.commit('saveUserInfo', {});
  userInfo.value = {};
  router.push('/login');
};

const getNoticeCount = async () => {
  try {
    const count = await $api.noticeCount();
    store.commit('saveNoticeCount', count);
  } catch (error) {
    console.error(error);
  }
};
const getMenuList = async () => {
  try {
    // const list = await $api.getMenuList();
    // 获取对应的权限菜单
    const { menuList, actionList } = await $api.getPermissionList();
    data.userMenu = menuList;
    store.commit('saveUserMenu', menuList);
    store.commit('saveUserAction', actionList);
  } catch (error) {
    console.error(error);
  }
};
getNoticeCount();
getMenuList();

const { isCollapse, userMenu } = toRefs(data);
</script>

<style lang="scss">
.basic-layout {
  position: relative;
  .nav-side {
    position: fixed;
    width: 200px;
    height: 100vh; // vh自动计算高度
    background-color: #001925;
    color: #fff;
    overflow-y: auto;
    transition: width 0.5s; // transition动画效果
    .logo {
      display: flex;
      align-items: center;
      font-size: 18px;
      height: 50px;
      img {
        margin: 0 16px;
        width: 32px;
        height: 32px;
      }
    }
    .nav-menu {
      height: calc(100vh - 50px);
      border-right: none;
    }
    // 合并
    &.fold {
      width: 64px;
    }
    // 展开
    &.unfold {
      width: 200px;
    }
  }
  .content-right {
    margin-left: 200px;
    // 合并
    &.fold {
      margin-left: 64px;
    }
    // 展开
    &.unfold {
      margin-left: 200px;
    }
    .nav-top {
      height: 50px;
      line-height: 50px;
      display: flex;
      justify-content: space-between; // 控制两端对齐
      border: 1px solid #ddd;
      padding: 0 20px;
      .nav-left {
        display: flex;
        align-items: center;
        .menu-fold {
          margin-right: 15px;
          font-size: 18px;
        }
      }
      .user-info {
        .notice {
          line-height: 30px;
          margin-right: 25px;
          cursor: pointer;
        }
        .user-link {
          cursor: pointer;
          color: #409eff;
        }
      }
    }
    .el-tabs__header.is-top {
      margin-bottom: 0;
    }
    .wrapper {
      background-color: #eef0f3;
      padding: 15px 20px 20px 20px;
      height: calc(
        100vh - 91px
      ); // 100vh 整个页面高度减去 nav：50px 和 padding(不计算在内) 的高度
      .main-page {
        padding: 15px;
        height: 100%;
        background-color: #fff;
      }
    }
  }
}
</style>
